//
//  BManagerViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/7/8.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BManagerViewController.h"
#import "AinvitiveListTableViewCell.h"
#import "AinvitivePersonModel.h"
#import "BinvishopDetaileViewController.h"
#import "ABshopDetaileViewController.h"
#import "BInviteManagerDetailVC.h"

@interface BManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@property(nonatomic,assign)NSInteger dayind;

@end

@implementation BManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    // Do any additional setup after loading the view.
    [self request];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refashaction:) name:@"refashOrder" object:nil];
}
-(void)refashaction:(NSNotification *)noti{
    [self request];
    
    
}
-(void)request{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    [NetWorkTool POST:devRepAndShop param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        self.dayind = [[[dic objectForKey:@"data"] objectForKey:@"day"] integerValue];
        for (NSMutableDictionary *mudic in [[dic objectForKey:@"data"] objectForKey:@"agentList"]) {
            [DYModelMaker DY_makeModelWithDictionary:mudic modelKeyword:@"" modelName:@""];
            AinvitivePersonModel *model = [[AinvitivePersonModel alloc]init];
            [model setValuesForKeysWithDictionary:mudic];
            [self.mydateSource addObject:model];
        }
        if (self.mydateSource.count == 0) {
            ShowToastWithText(@"暂无数据");
        }
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
        
        
    } needUser:YES];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mydateSource.count;;
}

//
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 94;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AinvitiveListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AinvitiveListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AinvitivePersonModel *model = [_mydateSource safeObjectAtIndex:indexPath.row];
    
    [cell refash:model];
    return cell;
}

-(NSDate *)nsstringConversionNSDate:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datestr = [dateFormatter dateFromString:dateStr];
    return datestr;
}
-(NSString *)dateConversionTimeStamp:(NSDate *)date
{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
    return timeSp;
}

- (BOOL)timeAgoWithDatedfdsfsf:(NSString*)date
{
    
    date=[NSString stringWithFormat:@"%@",date];
    
    NSString *datrStr = [NSString stringWithFormat:@"%@",date];
    NSString*time =[datrStr substringToIndex:datrStr.length-3];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[time intValue]];
    
    double deltaSeconds = fabs([confromTimesp timeIntervalSinceDate:[NSDate date]]);
    double deltaMinutes = deltaSeconds / 60.0f;
    
    if (deltaMinutes < (24 * 60 * self.dayind)){
        
        //        int day = (int)floor(deltaMinutes/(60 * 24));
        return YES;
        
    }else{
        return NO;
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AinvitivePersonModel *model = [_mydateSource safeObjectAtIndex:indexPath.row];
    BInviteManagerDetailVC *VC= [[BInviteManagerDetailVC alloc]init];
    VC.shopid = model.Id;
    [self.navigationController pushViewController:VC animated:YES];
    
}


-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        //        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //            [_myTableView.mj_header beginRefreshing];
        //        }];
        
        [_myTableView registerNib:[UINib nibWithNibName:@"AinvitiveListTableViewCell" bundle:nil] forCellReuseIdentifier:@"AinvitiveListTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
